﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;

namespace Moonlite.Data.Configuration
{
    /// <summary>
    /// Represents a generic enum converter.
    /// </summary>
    internal sealed class GenericEnumConverter : ConfigurationConverterBase
    {
        private Type enumType;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericEnumConverter"/> class.
        /// </summary>
        /// <param name="typeEnum">The enum type.</param>
        public GenericEnumConverter(Type typeEnum)
        {
            if (typeEnum == null)
            {
                throw new ArgumentNullException("typeEnum");
            }

            enumType = typeEnum;
        }

        /// <summary>
        /// Converts from the specified object.
        /// </summary>
        /// <param name="ctx">The type descriptor context.</param>
        /// <param name="ci">The culture info.</param>
        /// <param name="data">The object to convert from.</param>
        /// <returns>The converted object.</returns>
        public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data)
        {
            object obj2 = null;
            try
            {
                string str = (string)data;
                if (string.IsNullOrEmpty(str))
                {
                    throw new Exception();
                }

                if (!string.IsNullOrEmpty(str) && ((char.IsDigit(str[0]) || (str[0] == '-')) || (str[0] == '+')))
                {
                    throw new Exception();
                }

                if (str != str.Trim())
                {
                    throw new Exception();
                }

                obj2 = Enum.Parse(this.enumType, str);
            }
            catch
            {
                StringBuilder builder = new StringBuilder();
                foreach (string str2 in Enum.GetNames(this.enumType))
                {
                    if (builder.Length != 0)
                    {
                        builder.Append(", ");
                    }

                    builder.Append(str2);
                }

                throw new ArgumentException(string.Format("The enumeration value must be one of the following: {0}.", new object[] { builder.ToString() }));
            }

            return obj2;
        }

        /// <summary>
        /// Converts the specified value.
        /// </summary>
        /// <param name="ctx">The type descriptor context.</param>
        /// <param name="ci">The cultire info.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="type">The type to convert to.</param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
        {
            return value.ToString();
        }
    }
}
